#include "inteltimer_NativeStopwatch.h"

unsigned long start_high;
unsigned long start_low;

JNIEXPORT void JNICALL Java_inteltimer_NativeStopwatch_start
	(JNIEnv *, jobject){

	start_high = 0;
	start_low = 0;

	__asm{
		sub eax, eax
		sub edx, edx
		rdtsc
		mov start_low, eax
		mov start_high, edx
	}
}

JNIEXPORT jlong JNICALL Java_inteltimer_NativeStopwatch_stop
	(JNIEnv *, jobject){
	
	unsigned long time_high;
	unsigned long time_low;

	__asm{
		sub eax, eax
		sub edx, edx
		rdtsc
		sub eax, start_low
		sbb edx, start_high
		mov time_low, eax
		mov time_high, edx
	}
	
	return time_high == 0 ? time_low : 0;
}